//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Spreadsheet.Extensibility;
using OpenLS.Spreadsheet.Internals;

namespace OpenLS.Spreadsheet
{
    partial class Worksheet
    {
        class SetDefaultColumnWidth : WorksheetLevelUndoUnit
        {
            public SetDefaultColumnWidth(Worksheet ws, double? value)
                : base(ws)
            {
                this.value = value;
            }

            readonly double? value;
            public override void Do()
            {
                Worksheet._defaultColumnWidth = value;
            }
        }





        public void ClearRules()
        {
            foreach (var c in ConditionalFormatting)
                c.UnApply();
            ClearConditionalFormatting();
        }

        public void ClearRules(Range range)
        {
            var l = new List<ConditionalFormatting>();
            foreach (var c in ConditionalFormatting)
            {
                if (Range.Intersects(range, c.Range))
                    l.Add(c);
            }
            foreach (var c in l)
            {
                RemoveConditionalFormatting(c);
            }
            
        }

        public void ApplyFormattingRule(ConditionalFormattingRule formattingRule, Range range)
        {
            using (Workbook.CreateUndoScope(StringTable.ConditionalFormatting))
            {
                var conditionalFormatting = new ConditionalFormatting { Range = range };
                conditionalFormatting.Rules.Add(formattingRule);
                AddConditionalFormatting(conditionalFormatting);   
            }
        }
    }
}
